package org.steven0lisa.typhoon.ui;

import java.util.List;

import net.jingkao.app.common.Logger;
import net.jingkao.app.common.ThreadUtils;
import net.jingkao.app.common.ThreadUtils.UITask;
import net.jingkao.app.common.WebServices;

import org.steven0lisa.typhoon.R;
import org.steven0lisa.typhoon.bean.Typhoon;
import org.steven0lisa.typhoon.bean.TyphoonStatus;
import org.steven0lisa.typhoon.models.TyphoonModel;
import org.steven0lisa.typhoon.ws.Typhoons;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;

public class MainActivity extends BaseActivity {

	static final String key = "5fea0e598fd58e689c76de94a8b78f5b";
	
	static MainActivity instance = null;

	BMapManager mBMapMan = null;

	MapView mMapView = null;
	
	TyphoonLocationOverlay locationOverlay = null;
	
	TyphoonPathOverlay pathOverlay = null;
	
	TyphoonWindOverlay windOverlay = null;
	
	FeedbackAgent agent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		instance = this;
		
		agent = new FeedbackAgent(this);
		
		this.addOptionsMenu(new OptionsMenu() {

			@Override
			public int text(Menu menu) {
				return R.string.action_feedback;
			}

			@Override
			public void onAction(MenuItem item) {
			    agent.startFeedbackActivity();
			}

		});
		
		this.addOptionsMenu(new OptionsMenu() {

			@Override
			public int text(Menu menu) {
				return R.string.action_about;
			}

			@Override
			public void onAction(MenuItem item) {
				UIs.showAbout(instance);
			}

		});

		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init(key, null);
		// 注意：请在试用setContentView前初始化BMapManager对象，否则会报错
		setContentView(R.layout.activity_main);
		
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		// 设置启用内置的缩放控件
		MapController mMapController = mMapView.getController();
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point = new GeoPoint((int) (23.1333 * 1E6),
				(int) (113.2667 * 1E6));
		// 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point);// 设置地图中心点
		mMapController.setZoom(6);// 设置地图zoom级别
		
		Drawable mark= getResources().getDrawable(R.drawable.icon_marka);
		locationOverlay = new TyphoonLocationOverlay(mark, mMapView);
		pathOverlay = new TyphoonPathOverlay(mMapView);
		windOverlay = new TyphoonWindOverlay(mMapView);
		
        mMapView.getOverlays().add(pathOverlay);
		mMapView.getOverlays().clear();
		mMapView.getOverlays().add(windOverlay);
		mMapView.getOverlays().add(pathOverlay);
		mMapView.getOverlays().add(locationOverlay);
		
		
		ThreadUtils.execute(new UITask<List<Typhoon>>() {

			@Override
			public List<Typhoon> fetchData() {
				Typhoons ws = WebServices.proxy(Typhoons.class);
				return ws.listTyphoons();
			}

			@Override
			public void handleData(List<Typhoon> data) {
				if(data == null || data.isEmpty()){
					Toast.makeText(MainActivity.this, "暂时没有台风数据", Toast.LENGTH_SHORT).show();
					return;
				}
				TyphoonModel.saveTyphoons(data);
				for(Typhoon t: data){
					new TyphoonTracer(t).execute();
				}
			}
		});
		
		//启动友盟更新
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		
		agent.sync();
	}

	@Override
	protected void onDestroy() {
		mMapView.destroy();
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}
	
	public void updateMap(){
		if(mMapView != null){
			mMapView.refresh();
		}
	}
	
	class TyphoonTracer extends ThreadUtils.UITask<List<TyphoonStatus>>{
		
		Typhoon typhoon;
		
		TyphoonTracer(Typhoon typhoon){
			this.typhoon = typhoon;
		}

		@Override
		public List<TyphoonStatus> fetchData() {
			Typhoons ws = WebServices.proxy(Typhoons.class);
			List<TyphoonStatus> statusList = ws.getStatus(typhoon.typhoon_id);
			return statusList;
		}

		@Override
		public void handleData(List<TyphoonStatus> data) {
			Logger log = Logger.getLog(this);
			int index = 0;
			if(data != null){
				for(TyphoonStatus status: data){
					try{
						TyphoonLocationOverlayItem item = new TyphoonLocationOverlayItem(MainActivity.this, status.getPoint(),status, windOverlay);
						item.setAnchor(OverlayItem.ALING_CENTER);
						if(index == data.size() - 1){
							item.setMarker(getResources().getDrawable(R.drawable.typhoon));
						}
						locationOverlay.addItem(item);
					}catch(Exception e){
						log.e(e);
					}
					index ++;
				}
				pathOverlay.drawPath(data);
				mMapView.refresh();
			}else{
				Toast.makeText(MainActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
			}
			
		}
		
		public void execute(){
			ThreadUtils.execute(this);
		}
		
	}

}
